package com.llkj.qingnianguoxue.util.helper;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;


/**
 * rv刷新数据
 * Created by zhu.px on 2016/12/21.
 */

public class LoadDataHelper {

    /**
     * 下拉刷新成功加载到数据后调用
     */
    @SuppressWarnings("unchecked")
    public static <T> void refreshComplete(BaseQuickAdapter adapter, List<T> list, SwipeRefreshLayout swipeRefreshLayout,
                                           RecyclerView recyclerView) {
        adapter.setNewData(list);
        adapter.setEnableLoadMore(true);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.setEnabled(true);
        }
    }

    /**
     * 上拉加载的时候调用
     *
     * @param loadMoreListener
     * @param adapter
     * @param recyclerView
     * @param totalSize
     */
    public static void loadMoreRequest(final LoadMoreListener loadMoreListener, final BaseQuickAdapter adapter, final SwipeRefreshLayout swipeRefreshLayout,
                                       RecyclerView recyclerView, final int totalSize) {
        if (swipeRefreshLayout != null)
            swipeRefreshLayout.setEnabled(false);
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                if (adapter.getData().size() >= totalSize) {
                    adapter.loadMoreEnd();
                    if (swipeRefreshLayout != null)
                        swipeRefreshLayout.setEnabled(true);
                } else {
                    if (loadMoreListener != null)
                        loadMoreListener.onLoadMore();
                }
            }
        });
    }

    public interface LoadMoreListener {
        void onLoadMore();
    }
}
